package com.example.demoproject.controller;

import com.example.demoproject.common.BaseResponse;
import com.example.demoproject.common.ErrorCode;
import com.example.demoproject.model.dto.OrderDto;
import com.example.demoproject.model.vo.OrderVo;
import com.example.demoproject.utils.OrderUtils;
import com.example.demoproject.utils.ResultUtils;
import com.example.exception.OrderException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author TheOutsider
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
@Slf4j
public class OrderController {
    /**
     * 根据接受的data和k[], 返回结果(包含五个参数)
     * @param order
     * @return
     */
    @PostMapping("/info")
    public BaseResponse<List<OrderVo>> getAllInfo(@RequestBody OrderDto order) {
        // 参数校验
        if (order.getData() < 0) {
            throw new OrderException(ErrorCode.PARAM_ERROR, "请求数据中时间参数错误");
        }
        if (order.getK().size() < 1) {
            throw new OrderException(ErrorCode.PARAM_ERROR, "请求参数报告数量错误");
        }

        List<OrderVo> results = new ArrayList<>();
        Date date = new Date(order.getData());
        order.getK().forEach(item -> {
            OrderVo result = OrderUtils.getInfo(date, item);
            log.info(order.toString());
            results.add(result);
        });
        return ResultUtils.success(results);
    }
}
